package bento.api.virtualDB;

import static org.junit.Assert.*;
import bento.api.IBentoClient;
import bento.api.IBentoClient.IStoreManager;
import bento.api.IBentoClient.IStoreManager.IStore;
import bento.api.IBentoClient.IStoreManager.IStore.IBentoManager;

public class TestStore {
    
    public static void main(String[] args) {
        IBentoClient bc = createFixedBentoClient_1Store();
        System.out.println(bc.getStores());
    }
    
    private static IBentoClient createFixedBentoClient_1Store() {
        IBentoClient bc= new BentoClient();
        {
            IStoreManager mg = bc.getStores();
            {
                IStore store= mg.create().setName("s1").setTel("xx-0000001");
                {
                    IBentoManager bs = store.getBentos();
                    bs.add(bs.create().setName("s1-bento1").setPrice(50));
                    bs.commit();
                }
                {
                    IBentoManager bs = store.getBentos();
                    bs.add(bs.create().setName("s1-bento2").setPrice(55));
                    bs.commit();
                }
                mg.add(store);
                System.out.print("");
            }
            
            mg.commit();
            System.out.print("");
            //mg.clear();
            //mg.refresh();
        }
        
        return bc;
    }
    
    @org.junit.Test
    public void testAdd() {
        System.out.println();
        System.out.println("TestStore.testAdd: ");
        
        IBentoClient bc= createFixedBentoClient_1Store();
        System.out.println(bc.getStores());
        assertTrue(bc.getStores().size()==1);
        {
            IStore store= bc.getStores().create();
            store.setName("Store1");
            store.setTel("123456789");
            
            System.out.println("TestStore.testAdd: add");
            bc.getStores().add(store);
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
        }
        
        
        return;
        //fail("Not yet implemented");
    }
    
    @org.junit.Test
    public void testAddRefresh() {
        System.out.println();
        System.out.println("TestStore.testAddRefresh: ");
        
        IBentoClient bc= createFixedBentoClient_1Store();
        System.out.println(bc.getStores());
        assertTrue(bc.getStores().size()==1);
        {
            IStore store= bc.getStores().create();
            store.setName("Store1");
            store.setTel("123456789");
            
            System.out.println("TestStore.testAddRefresh: add ");
            bc.getStores().add(store);
            assertTrue(bc.getStores().size()==2);
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
            
            System.out.println("TestStore.testAddRefresh: refresh ");
            bc.getStores().refresh();
            //assertTrue(bc.getStores().size()==1);
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==1);
        }
        
        return;
        //fail("Not yet implemented");
    }
    
    @org.junit.Test
    public void testAddCommit() {
        System.out.println();
        System.out.println("TestStore.testAddCommit: ");
        
        IBentoClient bc= createFixedBentoClient_1Store();
        System.out.println(bc.getStores());
        assertTrue(bc.getStores().size()==1);
        {
            IStore store= bc.getStores().create();
            store.setName("Store1");
            store.setTel("123456789");
            
            System.out.println("TestStore.testAddCommit: add");
            bc.getStores().add(store);
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
            
            System.out.println("TestStore.testAddCommit: commit");
            bc.getStores().commit();
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
        }
        
        return;
        //fail("Not yet implemented");
    }

    
    @org.junit.Test
    public void testAddCommitRemoveRefresh() {
        System.out.println();
        System.out.println("TestStore.testAddCommitRemoveRefresh: ");
        
        IBentoClient bc= createFixedBentoClient_1Store();
        System.out.println(bc.getStores());
        assertTrue(bc.getStores().size()==1);
        {
            IStore store= bc.getStores().create();
            store.setName("Store1");
            store.setTel("123456789");
            assertTrue(bc.getStores().size()==1);

            System.out.println("TestStore.testAddCommitRemoveRefresh: add");
            bc.getStores().add(store);
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
            
            System.out.println("TestStore.testAddCommitRemoveRefresh: commit");
            bc.getStores().commit();
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
            
            System.out.println("TestStore.testAddCommitRemoveRefresh: remove");
            bc.getStores().remove(store);
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==1);
            
            System.out.println("TestStore.testAddCommitRemoveRefresh: refresh");
            bc.getStores().refresh();
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==2);
        }
        
        return;
        //fail("Not yet implemented");
    }

    @org.junit.Test
    public void testAddCommitReplaceRefresh() {
        System.out.println();
        
        
        {
            System.out.println("TestStore.testAddCommitReplaceRefresh1: ");
            IBentoClient bc= createFixedBentoClient_1Store();
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==1);
            {
                IStore store= bc.getStores().create();
                store.setName("Store1");
                store.setTel("123456789");
                assertTrue(store.getName().equals("Store1"));
                assertTrue(store.getTel().equals("123456789"));
                
                System.out.println("TestStore.testAddCommitReplaceRefresh1: add");
                bc.getStores().add(store);
                System.out.println(bc.getStores());
                assertTrue(bc.getStores().size()==2);
                
                System.out.println("TestStore.testAddCommitReplaceRefresh1: commit");
                bc.getStores().commit();
                System.out.println(bc.getStores());
                assertTrue(bc.getStores().size()==2);
                
                System.out.println("TestStore.testAddCommitReplaceRefresh1: replace");
                store.setName("Store1 - Replace");
                store.setTel("123456789 - Replace");
                System.out.println(bc.getStores());
                assertTrue(store.getName().equals("Store1 - Replace"));
                assertTrue(store.getTel().equals("123456789 - Replace"));
                assertTrue(bc.getStores().size()==2);
                
                System.out.println("TestStore.testAddCommitReplaceRefresh1: refresh");
                store.refresh();
                System.out.println(bc.getStores());
                assertTrue(store.getName().equals("Store1"));
                assertTrue(store.getTel().equals("123456789"));
                assertTrue(bc.getStores().size()==2);
            }
        }
        
        {
            System.out.println("TestStore.testAddCommitReplaceRefresh2: ");
            IBentoClient bc= createFixedBentoClient_1Store();
            System.out.println(bc.getStores());
            assertTrue(bc.getStores().size()==1);
            {
                IStore store= bc.getStores().create();
                store.setName("Store1");
                store.setTel("123456789");
                
                System.out.println("TestStore.testAddCommitReplaceRefresh2: add");
                bc.getStores().add(store);
                System.out.println(bc.getStores());
                assertTrue(bc.getStores().size()==2);
                
                System.out.println("TestStore.testAddCommitReplaceRefresh2: commit");
                bc.getStores().commit();
                System.out.println(bc.getStores());
                assertTrue(bc.getStores().size()==2);
                
                System.out.println("TestStore.testAddCommitReplaceRefresh2: replace");
                store.setName("Store1 - Replace");
                store.setTel("123456789 - Replace");
                System.out.println(bc.getStores());
                assertTrue(store.getName().equals("Store1 - Replace"));
                assertTrue(store.getTel().equals("123456789 - Replace"));
                assertTrue(bc.getStores().size()==2);
                
                System.out.println("TestStore.testAddCommitReplaceRefresh2: refresh");
                bc.getStores().refresh();
                System.out.println(bc.getStores());
                assertTrue(bc.getStores().get(bc.getStores().size()-1).getName().equals("Store1"));
                assertTrue(bc.getStores().get(bc.getStores().size()-1).getTel().equals("123456789"));
                assertTrue(bc.getStores().size()==2);
            }
        }
        
        return;
        //fail("Not yet implemented");
    }
    
    @org.junit.Test
    public void testAddCommitReplaceCommit() {
        System.out.println();
        System.out.println("TestStore.testAddCommitReplaceCommit: ");
        
        BentoClient bc= new BentoClient();
        {
            IStore store= bc.getStores().create();
            store.setName("Store1");
            store.setTel("123456789");
            assertTrue(bc.getStores().size()==0);
            
            System.out.println("TestStore.testAddCommitReplaceCommit.add: ");
            bc.getStores().add(store);
            assertTrue(bc.getStores().size()==1);
            
            System.out.println("TestStore.testAddCommitReplaceCommit.commit: ");
            bc.getStores().commit();
            assertTrue(bc.getStores().size()==1);
            
            for (IStore s : bc.getStores()) {
                System.out.println(s.toString());
            }
            assertTrue(bc.getStores().size()==1);
            
            System.out.println("TestStore.testAddCommitReplaceCommit.replace: ");
            store.setName("Store1 - Replace");
            store.setTel("123456789 - Replace");
            store.commit();
            System.out.println(store.toString());
            assertTrue(store.getName().equals("Store1 - Replace"));
            assertTrue(store.getTel().equals("123456789 - Replace"));
            
        }
        
        return;
        //fail("Not yet implemented");
    }
    
    @org.junit.Test
    public void testAddCommitReplaceCommitRefresh() {
        System.out.println();
        System.out.println("TestStore.testAddCommitReplaceCommitRefresh: ");
        
        BentoClient bc= new BentoClient();
        {
            IStore store= bc.getStores().create();
            store.setName("Store1");
            store.setTel("123456789");
            assertTrue(bc.getStores().size()==0);
            
            System.out.println("TestStore.testAddCommitReplaceCommitRefresh.add: ");
            bc.getStores().add(store);
            assertTrue(bc.getStores().size()==1);
            
            System.out.println("TestStore.testAddCommitReplaceCommitRefresh.commit: ");
            bc.getStores().commit();
            assertTrue(bc.getStores().size()==1);
            
            for (IStore s : bc.getStores()) {
                System.out.println(s.toString());
            }
            assertTrue(bc.getStores().size()==1);
            
            System.out.println("TestStore.testAddCommitReplaceCommitRefresh.replace: ");
            store.setName("Store1 - Replace");
            store.setTel("123456789 - Replace");
            System.out.println(store.toString());
            assertTrue(store.getName().equals("Store1 - Replace"));
            assertTrue(store.getTel().equals("123456789 - Replace"));
            
            System.out.println("TestStore.testAddCommitReplaceCommitRefresh.commit: ");
            store.commit();
            System.out.println(store.toString());
            assertTrue(store.getName().equals("Store1 - Replace"));
            assertTrue(store.getTel().equals("123456789 - Replace"));
            
            System.out.println("TestStore.testAddCommitReplaceCommitRefresh.refresh: ");
            store.refresh();
            System.out.println(store.toString());
            assertTrue(store.getName().equals("Store1 - Replace"));
            assertTrue(store.getTel().equals("123456789 - Replace"));
            
        }
        
        return;
        //fail("Not yet implemented");
    }
}
